<template>
  <!--1.首先，弹窗页面中要有el-dialog组件即弹窗组件，我们把弹窗中的内容放在el-dialog组件中-->
  <!--2.设置:visible.sync属性，动态绑定一个布尔值，通过这个属性来控制弹窗是否弹出-->
  <el-dialog title="编辑公告" :visible.sync="detailVisible" width="35%" :style="{borderRadius: '25px',padding: '10px'}">
    <div>
      <form>
        <div class="outline">
          <div>
            <label for="title">公告标题:</label>
            <input type="text" id="title" v-model="formMsg.title">
          </div>
          <div>
            <label for="content" >公告详情:</label>
            <textarea id="content" v-model="formMsg.content"></textarea>
          </div>
          <div>
            <label for="condition">公告状态:</label>
            <input type="radio" name="condition" value="公布" id="public" v-model="formMsg.publish">
            <label for="public">公布</label>
            <input type="radio" name="condition" value="不公布" id="private" v-model="formMsg.publish">
            <label for="private">不公布</label>
          </div>
          <div>
            <input type="button" value="取消" class="cancel" @click="detailVisible=false">
            <input type="button" value="保存" class="store" @click="onSubmit">
          </div>
        </div>
      </form>
    </div>
  </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  name: "dialogComponent",
  data() {
    return {
      detailVisible: false,
      announcement_id:"",
      formMsg:{
        title:"",
        content:"",
        condition:"",
      }
    }
  },
  methods: {
    //3.定义一个init函数，通过设置detailVisible值为true来让弹窗弹出，这个函数会在父组件的方法中被调用
    init(infor_id,title,content,status) {
      this.detailVisible = true;
      this.formMsg.title=title
      this.formMsg.content=content
      this.formMsg.condition=status
      this.announcement_id=infor_id
    },

    onSubmit(){
      axios({
        method:"get",
        url:"http://localhost:5000//admin/announcement/edit",
        params:{
          title:this.formMsg.title,
          content:this.formMsg.content,
          publish:this.formMsg.condition,
          admin_id:"27",
          announcement_id:this.announcement_id
        }
      }).then((res)=> {
        if(res.data.data ==='公告信息更新成功'){
          this.$router.go(0)
          this.$message({
            message: res.data.data,
            type:"success"
          })
          this.detailVisible = false
          //这里需要手动刷新
        }else {
          this.$message({
            message: res.data.data,
            type: "info"
          })

        }
      })
    }
  }
}
</script>
<style scoped>
el-dialog { border-radius: 10px; }
.outline{
  padding: 5%;
  display: flex;
  flex-direction: column;
  width: 450px;
  height: 320px;
}
.outline>div{
  margin: 3%;
}
label{
  vertical-align: top;
  margin-right: 2%;
}
#title{
  border-radius: 15px;
  background-color: rgb(237,237,237);
  margin-left: 5px;
  border: none;
  padding: 5px;
}
#content{
  width: 260px;
  height: 100px;
  resize: none ;
  border-radius: 15px;
  background-color: rgb(237,237,237);
  margin-left: 5px;
  border: none;
  padding-left: 5px;
  padding-right: 5px;
  font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif;

}
.cancel{
  display: inline-block;
  width: 100px;
  height: 40px;
  margin: 10px;
  position: relative;
  left: 40%;
  background-color: rgb(166,166,166);
  border-radius: 15px;
  border: none;
  color: white;
  cursor: pointer;
}
.cancel:hover{
  background-color: red;
}
.store{
  display: inline-block;
  width: 100px;
  height: 40px;
  margin: 10px;
  position: relative;
  left: 40%;
  background-color: rgb(125,153,191);
  border-radius: 15px;
  border: none;
  color: white;
  cursor: pointer;
}
.store:hover{
  background-color: rgb(85,153,191);
}
</style>